//
//  AppDelegate+First.m
//  DoneTask
//
//  Created by NeiQuan on 16/7/29.
//  Copyright © 2016年 Mr-yuwei. All rights reserved.
//

#import "AppDelegate+First.h"
#import "YWHeader.h"
#import "JPUSHService.h"
#import "GoodListDataManger.h"
#import "NSString+YWExtension.h"
#import "YWStaticPublicHeader.h"
#include <objc/runtime.h>
#import <AdSupport/AdSupport.h>
#import "JOJOWangDiamondConstants.h"
#import "JOJOWangSDK.h"
static const void *YWLaunchAlertKey = &YWLaunchAlertKey;
@implementation AppDelegate (First)
//应用每次启动不存在就重新注册设备
-(void)firstapplicationWillEnterForeground:(UIApplication *)application{
    
    
    if ([[ ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled])//不知道能否被注册
    {
        NSDictionary *parameters=@{@"deviceBrand":@"苹果手机",@"deviceModel":[NSString phoneModel],@"idfa":[ NSString idfa],@"ip":[NSString getIPAddress],@"mac":[NSString  macaddress],@"network":@([ NSString getNetWorkStates]),@"openudid":[ NSString  createUUID],@"systemVersion":[NSString deviceVersion],@"type":@"1",@"imei":@""};
        [GoodListDataManger  netWorkGetWithDataType:NetworkDatalaunchType parameters:parameters SuccessBlock:^(NSDictionary *dic)
         {
             if ([dic[@"code"]integerValue]==0)//创建用户成功   //每次进入APP时都需要调用，创建成功则返回唯一新设备Id，失败返回旧设备Id
             {
                 [[ NSUserDefaults standardUserDefaults] setObject:[dic[@"response"] stringValue] forKey:userId];//保存用户id
                 [[ NSUserDefaults standardUserDefaults] setBool:YES forKey:userlogin];
                 [[ NSUserDefaults standardUserDefaults] synchronize];
              
                 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DiamondConnectSuccess:) name:JOJOWang_CONNECT_SUCCESS_NOTIFICATION object:nil];
                 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DiamondConnectFailed:) name:JOJOWang_CONNECT_FAILED_NOTIFICATION object:nil];
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"haveLogin" object:nil];//告诉用户已经可以启动
                  [JOJOWangSDK requestJOJOWangSession:@"223efb0b3c2d73877e3fd49bba90db14" withUserID:[dic[@"response"] stringValue]];
                  [self loadDianleData:[dic[@"response"] stringValue]];
                  [self postTokenID];
             }
             
         }];
    }else
    {
       [[NSNotificationCenter defaultCenter] postNotificationName:@"alertNotice" object:nil];
    }
       [[ NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postTokenID) name:@"havedeviceToken" object:nil];
}
#pragma mark --传递tokenId
-(void)postTokenID
{
   // NSString *tokenID=[[ NSUserDefaults standardUserDefaults] objectForKey:@"userDeviceToken"];
    NSString *deviceId=[[NSUserDefaults standardUserDefaults] objectForKey:userId];
    NSLog(@"12%@",[JPUSHService registrationID]);
    if (![NSString StringIsNULL:deviceId]&&![NSString StringIsNULL:[JPUSHService registrationID]]){
        NSDictionary *parameters=@{@"jPushRegistrationID":[JPUSHService registrationID],@"deviceId":deviceId};
        [GoodListDataManger   netWorkGetWithDataType:NetworkDataTokenID parameters:parameters SuccessBlock:^(NSDictionary *dic) {
            if ([dic[@"code"]integerValue]==0)
            {
              [[NSNotificationCenter defaultCenter] postNotificationName:@"haveLogin" object:nil];//告诉用户已经可以启动
                NSLog(@"传递成功");
            }
        }];
        
    }
}
-(void)loadDianleData:(NSString *)userid{//获取点乐数据
    [JOJOWangSDK requestJOJOWangDiamondData:1 result:^(NSArray *diamonds, NSError *error) {
        if (error == nil)
        {
            NSData *data=[NSJSONSerialization dataWithJSONObject:diamonds options:NSJSONWritingPrettyPrinted error:nil];
            NSString *jsonstring=[[ NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding];
            NSDictionary *dic=@{@"rawJsonString":jsonstring,@"deviceId":userid};
            
           [GoodListDataManger netWorkGetWithDataType:NetworkDatauploadTaskList parameters:dic SuccessBlock:^(NSDictionary *dic){
            [[ NSNotificationCenter defaultCenter] postNotificationName:@"haveLogin" object:nil];//告诉用户已经可以启动
           }];
            
        }
    }];
}
#pragma mark - JOJOWang diamond connect notifications
- (void)DiamondConnectSuccess:(NSNotification *)notification
{
    NSLog(@"点乐链接成功");
    
}
- (void)DiamondConnectFailed:(NSNotification *)notification
{
    
}
@end
